package com.lepu.app.fun.index.remind;

import com.app.utils.DateUtil;

import android.app.AlarmManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;

public class RemindReceiver extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {

		if(intent == null || TextUtils.isEmpty(intent.getAction()))
		{
			return;
		}

		Log.d("CUSTOM_TAG", "收到闹钟广播了    " + intent.getAction());

		if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED))
		{
			//设置最近的闹钟
			Alarm.setRemindNextAlarm(context);
		}else{

			Intent intentSound = new Intent(context, RemindSoundActivity.class);
			Alarm alarm = (Alarm) intent.getSerializableExtra("alarm");
			Bundle bundle = new Bundle();
			bundle.putSerializable("remindAlarm", alarm);
			intentSound.putExtras(bundle);
			intentSound.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			context.startActivity(intentSound);

			//收到监听，循环
			if (alarm != null && alarm.isRepeat){
				AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
				alarm.alarmTime += DateUtil.DAY_MILLIS;

				AlarmTipsManager.getInstance().updateAlarm(alarm);
				Alarm.setRemindNoDB(alarm, alarmManager, context);
			}
		}
	}
}